package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityPicDoodleBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicDoodleActivity extends BaseAc<ActivityPicDoodleBinding> {

    public static String imgPath = "";
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private ColorAdapter colorAdapter;
    private int oldPosition = 0;
    private Dialog myTipDialog;
    private boolean isShow=false;
    private boolean isEraser=false;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_doodle;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicDoodleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivDoodleSave.setOnClickListener(this);
        mDataBinding.ivDoodleBrushSel.setOnClickListener(this);
        mDataBinding.ivDoodleBrushNotSel.setOnClickListener(this);
        mDataBinding.rvDoodleColorList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvDoodleColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivDoodleSave://保存
                saveImg();
                break;
            case R.id.ivDoodleBrushSel://已选择
                if (isShow){
                    isShow=false;
                    mDataBinding.llDoodleBrushNotSel.setVisibility(View.GONE);
                }else {
                    isShow=true;
                    mDataBinding.llDoodleBrushNotSel.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ivDoodleBrushNotSel://未选择
                if (isEraser){
                    isEraser=false;
                    mDataBinding.ivDoodleBrushNotSel.setImageResource(R.drawable.xiangpi2);
                    mDataBinding.ivDoodleBrushSel.setImageResource(R.drawable.huabi1);
                    mDataBinding.llDoodleBrushNotSel.setVisibility(View.GONE);
                    mDataBinding.sbBrush.setVisibility(View.VISIBLE);
                    mDataBinding.sbEraser.setVisibility(View.GONE);
                    penBrush.setIsEraser(false);
                    penBrush.setSize(paintWidth);
                }else {
                    isEraser=true;
                    mDataBinding.ivDoodleBrushNotSel.setImageResource(R.drawable.huabi2);
                    mDataBinding.ivDoodleBrushSel.setImageResource(R.drawable.xiangpi1);
                    mDataBinding.llDoodleBrushNotSel.setVisibility(View.GONE);
                    mDataBinding.sbBrush.setVisibility(View.GONE);
                    mDataBinding.sbEraser.setVisibility(View.VISIBLE);
                    penBrush.setIsEraser(true);
                    penBrush.setSize(eraserWidth);
                }
                isShow=false;
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(oldPosition).setSelect(false);
        colorAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        colorAdapter.getItem(position).setSelect(true);
        colorAdapter.notifyItemChanged(position);
        penBrush.setColor(Color.parseColor(colorAdapter.getItem(position).getColor()));//默认画笔颜色
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.imageViewTouch);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        initPen();
        //颜色数据
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#333333", true));
        listColor.add(new MyColorBean("#FFFFFF", false));
        listColor.add(new MyColorBean("#E12D2D", false));
        listColor.add(new MyColorBean("#FFA8A8", false));
        listColor.add(new MyColorBean("#FFB652", false));
        listColor.add(new MyColorBean("#FFF62C", false));
        listColor.add(new MyColorBean("#79BA56", false));
        listColor.add(new MyColorBean("#35B9B0", false));
        listColor.add(new MyColorBean("#3F68BC", false));
        listColor.add(new MyColorBean("#9F45F1", false));
        listColor.add(new MyColorBean("#E85C90", false));
        listColor.add(new MyColorBean("#C0C0C0", false));
        listColor.add(new MyColorBean("#CA58C9", false));
        listColor.add(new MyColorBean("#8A72E1", false));
        listColor.add(new MyColorBean("#46D5AF", false));
        listColor.add(new MyColorBean("#5E8C4F", false));
        colorAdapter.setList(listColor);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlDoodleSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#333333"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小
        mDataBinding.sbBrush.setMax(50);
        mDataBinding.sbBrush.setProgress(5);
        mDataBinding.sbBrush.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //设置橡皮擦大小
        mDataBinding.sbEraser.setMax(50);
        mDataBinding.sbEraser.setProgress(5);
        mDataBinding.sbEraser.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        TextView textView=inflate.findViewById(R.id.tvDialogTipText);
        textView.setText(getString(R.string.image_not_save));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}